package com.ruoyi.custom.service.impl;

import com.ruoyi.custom.domain.vo.res.CarConfigResVo;
import com.ruoyi.custom.domain.vo.res.CarDetailResVo;
import com.ruoyi.custom.domain.vo.res.CarHomeListResVo;
import com.ruoyi.custom.domain.vo.res.CarQuestionResVo;
import com.ruoyi.custom.mapper.CarMapper;
import com.ruoyi.custom.service.ICarService;
import com.ruoyi.common.core.domain.car.CarList;
import com.ruoyi.common.core.domain.car.CarPageListReq;
import com.ruoyi.common.core.pub.PubSelect;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 参数配置 服务层实现
 *
 * @author ruoyi
 */
@Service
public class CarServiceImpl implements ICarService {

    @Resource
    private CarMapper carMapper;

    @Override
    public List<PubSelect> cateSelectList(String cateGroupCode) {
        return carMapper.cateSelectList(cateGroupCode);
    }

    @Override
    public CarHomeListResVo homeCarList(Long cateId) {
        Integer totalCarCount = carMapper.carTotalByCateId(cateId);
        List<CarList> carList = carMapper.carListByCateId(cateId).stream().peek(car -> {
            Long carId = car.getId();
            List<String> fileAddressList = carMapper.carFileListById(carId);
            if (CollectionUtils.isNotEmpty(fileAddressList)) {
                car.setCarFilePath(fileAddressList.get(0));
            }
        }).collect(Collectors.toList());
        return new CarHomeListResVo(totalCarCount, carList);
    }

    @Override
    public List<CarList> carPageList(CarPageListReq req) {
        return carMapper.carPageList(req).stream().peek(car -> {
            Long carId = car.getId();
            List<String> fileAddressList = carMapper.carFileListById(carId);
            if (CollectionUtils.isNotEmpty(fileAddressList)) {
                car.setCarFilePath(fileAddressList.get(0));
            }
        }).collect(Collectors.toList());
    }

    @Override
    public CarDetailResVo carDetailById(Long carId) {
        CarDetailResVo carDetailResVo = carMapper.carDetailById(carId);
        carDetailResVo.setFileAddressList(carMapper.carFileListById(carId));
        List<CarConfigResVo> allConfigList = carMapper.findAllConfigList();
        allConfigList.forEach(config -> {
            List<String> configValueList = carMapper.getCarConfigList(carId, config.getId());
            config.setConfigValueList(configValueList);
        });
        carDetailResVo.setConfigList(allConfigList);
        return carDetailResVo;
    }

    @Override
    public List<CarQuestionResVo> carQuestionList() {
        return carMapper.carQuestionList();
    }
}
